package com.baiyang.app.ui.mine;

import android.content.Context;

import com.alibaba.fastjson.JSONObject;
import com.baiyang.app.model.User;
import com.baiyang.app.tool.DataTool;
import com.baiyang.app.tool.LogUtils;
import com.baiyang.app.tool.net.SendMsgUtil;
import com.baiyang.app.tool.net.Urls;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Response;

public class MineService {

    private Context context;
    private MineViewModel viewModel;

    public MineService(Context context,MineViewModel viewModel){
        this.context = context;
        this.viewModel = viewModel;
    }


    /**
     * 获取书币和积分
     * @param user
     */
    public void getBookCoinAndIntegral(User user) {
        new SendMsgUtil(){
            @Override
            public void deal(Call call, Response response) throws IOException {
                if(response.code() == 200){
                    String str = response.body().string();
                    LogUtils.i("getBookCoinAndIntegral",str);
                    JSONObject object = JSONObject.parseObject(str);
                    String result = object.getString("result");
                    if("SUCCESS".equals(result)){
                        JSONObject obj = object.getJSONObject("data");
                        String integral = obj.getString("integral");
                        String bookCoin = obj.getString("bookCoin");
                        if(!DataTool.isBlank(integral)){
                            viewModel.setIntegral(integral);
                        }
                        if(!DataTool.isBlank(bookCoin)){
                            viewModel.setBookCoin(bookCoin);
                        }
                    }else{
                        String detail = object.getString("detail");
                        LogUtils.i("getBookCoinAndIntegral", detail);
                    }
                }else{
                    LogUtils.i("getBookCoinAndIntegral","code:" + response.code());
                }
            }

            @Override
            public void dealFailure(Call call, IOException e) throws IOException {
                LogUtils.i("getBookCoinAndIntegral","IOException:" + e.getLocalizedMessage());
            }
        }.sendPost(Urls.User.GET_BOOK_COIN_AND_INTEGRAL,user);
    }

    /**
     * 签到
     * @param user
     */
    public void signIn(User user) {
        new SendMsgUtil(){
            @Override
            public void deal(Call call, Response response) throws IOException {
                if(response.code() == 200){
                    String str = response.body().string();
                    LogUtils.i("signIn",str);
                    JSONObject object = JSONObject.parseObject(str);
                    viewModel.setSignInResult(object);
                }else{
                    LogUtils.i("signIn","code:" + response.code());
                }
            }

            @Override
            public void dealFailure(Call call, IOException e) throws IOException {
                LogUtils.i("signIn","IOException:" + e.getLocalizedMessage());
            }
        }.sendPost(Urls.USER_INTEGRAL.SIGN_IN,user);
    }

    /**
     * 充值
     * @param money
     * @param id
     */
    public void recharge(String money, String id) {
        JSONObject obj = new JSONObject();
        obj.put("userId",id);
        obj.put("money",money);
        new SendMsgUtil(){
            @Override
            public void deal(Call call, Response response) throws IOException {
                if(response.code() == 200){
                    String str = response.body().string();
                    LogUtils.i("signIn",str);
                    JSONObject object = JSONObject.parseObject(str);
                    viewModel.setRechargeResult(object);
                }else{
                    LogUtils.i("signIn","code:" + response.code());
                }
            }

            @Override
            public void dealFailure(Call call, IOException e) throws IOException {
                LogUtils.i("signIn","IOException:" + e.getLocalizedMessage());
            }
        }.sendPost(Urls.BALANCE.RECHARGE,obj);
    }
}
